<?php
/**
 * 广告管理
 */
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');

class AdvertAction extends AdministratorAction {

	public function _initialize(){
		$this->pageTitle['index'] = '所有广告';
		$this->pageTitle['checkAdvert'] = '未展示广告';
		$this->pageTitle['addAdvert'] = '未展示广告';
		parent::_initialize();
	}
	//广告列表
	public function index() {
		$_REQUEST['tabHash'] = 'index';
		// 初始化用户列表管理菜单
		$this->_listpk = 'aid';
		$this->_initAdvertListAdminMenu("index");
		// 数据的格式化与listKey保持一致
		$map ["is_del"] = 'n';
		$listData = $this->_getAdvertList('20', $map,"index");
		
		// 列表批量操作按钮
		$this->pageButton[] = array('title'=>"搜索广告",'onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>"批量取消展示",'onclick'=>"admin.offAdverts('')");
		//$this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_USER_GROUP'),'onclick'=>"admin.changeUserGroup()");
		// 转移用户部门，如果需要请将下面的注释打开
		// $this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_DEPARTMENT'),'onclick'=>"admin.changeUserDepartment()");
		//$this->assign('pageTitle',$isRec ? L('PUBLIC_RECYCLE_BIN'):L('PUBLIC_FILE_MANAGEMENT'));
		$this->displayList($listData);
		
	}
	public function checkAdvert(){
		$_REQUEST['tabHash'] = 'checkAdvert';
		// 初始化用户列表管理菜单
		$this->_initAdvertListAdminMenu("checkAdvert");
		// 数据的格式化与listKey保持一致
		$map ["is_del"] = 'n';
		$map ["status"] = 0;
		$listData = $this->_getAdvertList('20', $map,"checkAdvert");
		
		// 列表批量操作按钮
		$this->pageButton[] = array('title'=>"搜索广告",'onclick'=>"admin.fold('search_form')");
		//$this->pageButton[] = array('title'=>"批量删除",'onclick'=>"admin.delAdverts('')");
		//$this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_USER_GROUP'),'onclick'=>"admin.changeUserGroup()");
		// 转移用户部门，如果需要请将下面的注释打开
		// $this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_DEPARTMENT'),'onclick'=>"admin.changeUserDepartment()");
		$this->displayList($listData);
	}
	
	/**
	 * 初始化Advert列表管理菜单
	 * @param string $type 列表类型，index、noshow
	 */
	private function _initAdvertListAdminMenu($type) {
		// tab选项
		if($type == "index"){
			$this->pageTab[] = array('title'=>"展示中",'tabHash'=>'index','url'=>U('admin/Advert/index'));
			$this->pageTab[] = array('title'=>"未展示",'tabHash'=>'noshow','url'=>U('admin/Advert/noshow'));
		}
		
		
		// 搜索选项的key值
		// $this->searchKey = array('uid','uname','email','sex','department','user_group',array('ctime','ctime1'));
		$this->searchKey = array('aid','title',"is_banner",'status',array('ctime','ctime1'));
		// 针对搜索的特殊选项
		$this->opt['status'] = array('0'=>"未展示",'1'=>"展示中");
		$this->opt['is_banner'] = array('0'=>"列表",'1'=>"轮播图");
		$this->opt['is_link'] = array('0'=>"不是",'1'=>"是");
		
		$map['pid'] = array('NEQ', 0);
	
		// 列表key值 DOACTION表示操作
		$this->pageKeyList = array('aid','title','status','pic','is_link',"link",'ctime','DOACTION');
	
		/*		if(!empty($_POST['_parent_dept_id'])) {
		 $this->onload[] = "admin.departDefault('".implode(',', $_POST['_parent_dept_id'])."','form_user_department')";
		}*/
	}
	
	/**
	 * 解析用户列表数据
	 * @param integer $limit 结果集数目，默认为20
	 * @param array $map 查询条件
	 * @param string $type 格式化数据类型，index、pending、dellist
	 * @return array 解析后的用户列表数据
	 */
	private function _getAdvertList($limit = 20, $map = array(),$tab) {
		// 设置列表主键
		$this->_listpk = 'aid';
		// 取用户列表
		$listData = model('Advert')->getAdvertList($limit, $map);
		//dump($listData);exit;
		foreach($listData['data'] as $k => $v) {
			//获取用户组信息

			$image = D('attach')->where('attach_id='.$listData['data'][$k]['image'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			//$listData['data'][$k]['pic'] = getImageUrl($image_url);
			$listData['data'][$k]['pic'] = "<img style='height:100px;' src='".$listData['data'][$k]['pic']."'"." />";
			$listData['data'][$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
			if ($listData['data'][$k]['status'] == 0){
				$listData['data'][$k]['status'] = "待审核";
			}elseif ($listData['data'][$k]['status'] ==1){
				$listData['data'][$k]['status'] = "审核通过";
			}elseif($listData['data'][$k]['status'] == -1){
				$listData['data'][$k]['status'] = "审核不通过";
			}
			if($tab == "checkAdvert"){
				$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Advert/AdvertDetail',array('tabHash'=>'checkAdvert','id'=>$v['aid'])).'">'."查看详情".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.passAdvert(\''.$v['aid'].'\')">'."通过".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.unpassAdvert(\''.$v['aid'].'\')">'."不通过".'</a>';
			}else{
				//设置操作
				$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Advert/editAdvert',array('tabHash'=>'editAdvert','id'=>$v['aid'])).'">'.L('PUBLIC_EDIT').'</a> - ';
				$listData['data'][$k]['DOACTION'] .= $v['status'] == 1 ? '<a href="javascript:void(0)" onclick="admin.offAdvert(\''.$v['aid'].'\')">'.'下架'.'</a> - ' : '<a href="javascript:void(0)" onclick="admin.showAdvert(\''.$v['id'].'\')">'.'展示'.'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.delAdvert(\''.$v['aid'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			}
			
		}
		return $listData;
	}
	
	public function doDeleteAdvert(){
		
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode("没有数据"));
		}
		$str="";
		foreach ($_REQUEST as $k => $v){
			//$_REQUEST['id']={0=>1,1=>2}
			$str .= $k."+".$v."/";
		}
		
		
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = $str;			// 操作成功
		
		exit(json_encode($return));
	}
	
	//添加
	public function addAdvert() {
		
// 		dump($_SERVER);exit;
		// 初始化用户列表管理菜单
		$this->_initAdvertListAdminMenu();
		
		$this->pageKeyList = array("pic","is_link","is_banner","link","title","content","status");

		// 字段选项配置
		$this->opt['status'] = array('0'=>"待审核",'1'=>"审核中");
		$this->opt['is_banner'] = array('0'=>"列表",'1'=>"轮播图");
		//$this->opt['identity'] = array('1'=>L('PUBLIC_PERSONAL'),'2'=>L('PUBLIC_ORGANIZATION'));
		// $group = D('user_group')->where('is_authenticate=0')->findAll();
		// foreach($group as $k=>$v){
		// 	$unverifyGroup[$v['user_group_id']] = $v['user_group_name'];
		// }
		// $this->opt['user_group'] = $unverifyGroup;
		
		$map['pid'] = array('NEQ', 0);
		
		// 表单URL设置
		$this->savePostUrl = U('admin/Advert/doAddAdvert');
		//$this->notEmpty = array("image","status");
		$this->onsubmit = 'admin.addAdvertSubmitCheck(this)';
	
		$this->displayConfig();
	}
	public function doAddAdvert() {
		
		if(h(t($_POST['link'])) != ""){
			$res = preg_match('/^(?:https?|ftp):\/\/(?:www\.)?(?:[a-zA-Z0-9][a-zA-Z0-9\-]*)/',h($_POST['url']));
			if(!$res) $this->error('外链格式错误');
		}
		$Advert = model('Advert');
		$map = $Advert->create();
		$image_id = $map["pic"];
		$pic = model("Attach")->getAttachById($image_id);
		
		$map["pic"] = $pic["save_path"].$pic["save_name"];
		// 检查map返回值，有表单验证
		$result = $Advert->add($map);
		if($result) {
			$this->assign('jumpUrl', U('admin/Advert/index'));
			$this->success(L('PUBLIC_ADD_SUCCESS'));
		} else {
			$this->error($Advert->getLastError());
		}
	}
	
	/**
	 * 编辑广告页面
	 */
	public function editAdvert(){		
		$this->assign('pageTitle','编辑广告');
		
		$this->pageKeyList = array("pic","is_link","is_banner","link","title","content","status");
		$this->opt['status'] = array('0'=>"待审核",'1'=>"审核中");
		$this->opt['is_banner'] = array('0'=>"列表",'1'=>"轮播图");
		//$this->opt['essence'] = array('1'=>'是','0'=>'否');
		$Advert = model('Advert')->where('id='.$_GET['id'])->find();
		if($Advert['image']){
			$image = D('attach')->where('attach_id='.$Advert['image'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$Advert['image_url'] = getImageUrl($image_url);
		}
		//$this->notEmpty = array('image');	
		$this->savePostUrl = U('admin/Advert/doEditAdvert',array("id"=>$_GET['id']));
		
		$this->displayConfig($Advert);
	}
	
	public function doEditAdvert(){
		//dump($_REQUEST);exit;
		$Advert = M("Advert");
		$id = t($_REQUEST['id']);
		$Advert->pic = t($_REQUEST["pic"]);
		$Advert->title =t( $_REQUEST["title"]);
		$Advert->is_link = t($_REQUEST["is_link"]);
		$Advert->link = t($_REQUEST["link"]);
		$Advert->status = t($_REQUEST["status"]);
		
		if($Advert->where("id = ".$id)->save()){
			$this->assign('jumpUrl', U('admin/Advert/index'));
			$this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
		}else{
			$this->error($Advert->getError());
		}
	}
	public function doOffAdvert(){
		$Advert = M("Advert");
		$id = $_REQUEST['id'];
		$Advert->status = 0;
		if($Advert->where("id = $id")->save()){
			$return['data'] = "操作成功";
		}else{
			$return['data'] = "操作失败";
		}
	
		exit(json_encode($return));
	}
	public function doOffAdverts(){
		$Advert = M("Advert");
		$id = $_REQUEST['id'];
		$ss = "";
		foreach ($_REQUEST as $k => $v){
			$ss .=$k."+".$v."/";
		}
		
		
		$return['status'] = 1;
		$return['data'] = $ss;
		/*
		$str = "id in (";
		for($i=0;$i<count($id);$i++){
			$str .= $id[$i].',';
		}
		$str = trim($str,',');
		$str .= ")";
		$Advert->status = 0;
		if($Advert->where($str)->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}*/
		
		exit(json_encode($return));
	}
	public function doShowAdvert(){
		$Advert = M("Advert");
		$id = $_REQUEST['id'];
		$str = "id in (";
		for($i=0;$i<count($id);$i++){
			$str .= $id[$i].',';
		}
		$str = trim($str,',');
		$str .= ")";
		$Advert->status = 1;
		if($Advert->where($str)->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
		
		exit(json_encode($return));
		
	}
	
	public function dodelAdvert(){
		$Advert = M("Advert");
		$id = $_REQUEST['id'];
		
		if($Advert->where("id = $id")->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
	
		exit(json_encode($return));
	}
	public function dodelAdverts(){
		$Advert = M("Advert");
		$aid = $_REQUEST['aid'];
		$str = "id in (";
		for($i=0;$i<count($id);$i++){
			$str .= $id[$i].',';
		}
		$str = trim($str,',');
		$str .= ")";
		
		if($Advert->where($str)->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
		
		exit(json_encode($return));
	}
	
	public function doPassAD(){
		$Advert = M("Advert");
		$aid = $_REQUEST['aid'];
		$Advert->status = 1;
		if($Advert->where("aid = $aid")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
		
		exit(json_encode($return));
	}
	
	public function doUnpassAD(){
		$Advert = M("Advert");
		$aid = $_REQUEST['aid'];
		$Advert->status = -1;
		if($Advert->where("aid = $aid")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
	
		exit(json_encode($return));
	}
}
